/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.campaigns;


import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.database.sqlite.SQLiteDatabase;

public class Attachs {
	private String idAttach;
	private String size;
	private String fileName;
	String mime;
	private String descri;
	private String hash;
	Vector<Attachs> attachs=new Vector<Attachs>();
	
	public static Attachs[] fromDataBase(){return null;} //TODO
	
	public Vector<Attachs> fromXml(InputSource xml){
		
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new Attachs.MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml); 
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		
		return attachs;
	}

	
	class MyHandler extends DefaultHandler {
		String lastElement;
		boolean begin;
		boolean valid;
		
		int count=0;

		public void endElement(String uri, String localName, String qName) {
			begin=false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin=true;
			
			if(localName.compareTo("Attachment")==0){
				count++;
				attachs.add(new Attachs());
				valid=true;
			}
			lastElement=localName;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			if(valid){
				String s = new String(ch, start, length);
				if(lastElement==null)return;
				if(!begin)return;
				if (lastElement.compareTo("idAttachment")==0) {
					if(s.compareTo("null")!=0){
						Attachs att = attachs.get(count-1);
						att.setIdAttach(s);
					}
				}else if (lastElement.compareTo("filename")==0) {
					if(s.compareTo("null")!=0){
						Attachs invi = attachs.get(count-1);
						invi.setFileName(s);
					}
				}else if (lastElement.compareTo("size")==0) {
					if(s.compareTo("null")!=0){
						Attachs invi = attachs.get(count-1);
						invi.setSize(s);
					}
							
				}else if (lastElement.compareTo("description")==0) {
					if(s.compareTo("null")!=0){
						Attachs invi = attachs.get(count-1);
						invi.setDescri(s);
					}
			
				}else if (lastElement.compareTo("mimetype")==0) {
					if(s.compareTo("null")!=0){
						Attachs invi = attachs.get(count-1);
						invi.mime=s;
					}
			
				}else if (lastElement.compareTo("hash")==0) {
					if(s.compareTo("null")!=0){
						Attachs invi = attachs.get(count-1);
						invi.setHash(s);
					}				
				}
			}
		}
	}
		
		
	public String toXml(){return null;}//TODO
	
	public boolean toDataBase(SQLiteDatabase db, String user){
		return false;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getSize() {
		return size;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public String getHash() {
		return hash;
	}

	public void setDescri(String descri) {
		this.descri = descri;
	}

	public String getDescri() {
		return descri;
	}

	public void setIdAttach(String idAttach) {
		this.idAttach = idAttach;
	}

	public String getIdAttach() {
		return idAttach;
	}
	
	
}
